package com.xiangwushuo.framework.activity.web.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.lib.common.base.web.BaseWebFragment
import com.lib.common.constant.INTENT_KEY_TITILE
import com.lib.common.constant.INTENT_KEY_URL
import com.lib.common.entity.LoginEvent
import com.lib.framework.entity.Event
import com.lib.framework.manager.LogManager
import com.xiangwushuo.framework.R
import com.xiangwushuo.framework.databinding.FragmentWebBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

/**
 * Copyright (C), 2019-2020
 * FileName: WebFragment
 * Author: Kevin
 * Date: 2020-01-15 14:21
 * Description: web fragment
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class WebFragment : BaseWebFragment<WebViewModel>() {

    companion object {

        /**
         * 发给 fragment 的事件
         */
        const val EVENT_XXX = 1 // XXX 事件

        /**
         * 发送给 activity 的事件，activtiy 需要实现 SendEventToActivityListener 接口
         */
        const val FRAGMENT_EVENT_XXXX = 1 // XXXX事件

        @JvmStatic
        fun newInstance(title: String, url: String): WebFragment {
            return WebFragment().apply {
                arguments = Bundle().apply {
                    putString(INTENT_KEY_TITILE, title)
                    putString(INTENT_KEY_URL, url)
                }
            }
        }
    }

    private var mDataBinding: FragmentWebBinding? = null

    override fun getViewModelClass() = WebViewModel::class.java

    override fun getWebViewId() = R.id.dWebView

    override fun getProgressBarId() = R.id.progressBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        EventBus.getDefault().register(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        if (mDataBinding == null) {
            mDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_web, container,
                false)
            mDataBinding?.viewModel = mViewModel
        }

        return mDataBinding?.root
    }

    override fun lazyLoad() {
        initView()
        mViewModel.startLoadUrl()
    }

    override fun dealEvent(event: Event?): Boolean {
        // 处理 viewModel 发送过来的事件
        when (event?.code) {
            EVENT_XXX -> {
                val extraObj = event.extraObj
                if (extraObj is String) {
                    LogManager.i(mTag, "XXX 事件 的额外参数 extraObj = $extraObj")
                }
                return true
            }
        }

        return super.dealEvent(event)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe
    fun onLoginEvent(obj: LoginEvent) { // 登陆
        if (!TextUtils.isEmpty(mViewModel.mUrl)) {
            mViewModel.addParams()
            mViewModel.loadUrl(mViewModel.mUrl)
        }
    }
}